package cn.herlioner.action;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.AppConfigurationEntry;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.RequestMap;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.hql.ast.util.ASTUtil;



import antlr.collections.Stack;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Context;

import cn.herlioner.dao.StudentDAO;
import cn.herlioner.dao.impl.CollegeDAOImpl;
import cn.herlioner.dao.impl.Obj_StuDAOImpl;
import cn.herlioner.dao.impl.ObjectDAOImpl;
import cn.herlioner.dao.impl.StudentDAOImpl;
import cn.herlioner.pojo.CRUObject;
import cn.herlioner.pojo.Obj_Stu;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.pojo.Student;
import cn.herlioner.service.CRUObjectService;
import cn.herlioner.service.Obj_StuServiceImpl;
import cn.herlioner.service.PageService;

public class StudentAction extends ActionSupport implements SessionAware {
	Student student;
	StudentDAO studentDAO = new StudentDAOImpl();
	List<Student> students;
	Obj_Stu obj_Stu;
	Obj_StuDAOImpl obj_StuDAO = new Obj_StuDAOImpl();
	List<Obj_Stu> obj_Stus;
	TSObject tSObject;
	ObjectDAOImpl objectDAO = new ObjectDAOImpl();
	List<TSObject> tSObjects = new ArrayList();
	
	CRUObject cruObject; 
	List<CRUObject> cruObjects = new ArrayList();
	CRUObjectService cruObjectService = new CRUObjectService();
	
	private int currentPagechange;
	
	/**
	 * 获取单个学生的信息，打到让学生修改个人信息的目的
	 * @return
	 */
	public String getstuinfo(){
		//从session中获取学生学号
		Long stunum =  (Long) ActionContext.getContext().getSession().get("Num");
		HttpServletRequest request = ServletActionContext.getRequest();
		//用学生学号从数据库中读取对应的单个学生的信息
		student = studentDAO.get(stunum);
		
		if(student != null){
			request.setAttribute("student", student);
			return "getsuccess";
			}
		return "unsuccess";	
	}
	
	/**
	 * 学生用户获取学生信息和学生课程信�?
	 */
	public String getstudentself(){
		
		//获取学生信息
		Long stunum = (Long) ActionContext.getContext().getSession().get("Num");
		System.out.println(stunum);
		int pageSize = 5 ;
		Map session = ActionContext.getContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		if(request.getParameter("currentPagechange")==null){
			currentPagechange = (int)session.get("currentPagechange");
			System.out.println(currentPagechange);
		}else{
			currentPagechange = Integer.valueOf(request.getParameter("currentPagechange"));
		}
		
		PageService Pageservice1 = obj_StuDAO.getByPageByStunum(currentPagechange, pageSize,stunum);
		
    	//获取组合表里面的组合信息
		try{
			for (int i = 0 ;i<pageSize;i++) {
		    	CRUObject cruObject = new CRUObject();
		    	TSObject obj = (TSObject)Pageservice1.getRecordList().get(i);
		    	cruObject = cruObjectService.setobject(obj,cruObject);
		    	cruObjects.add(cruObject);
		    	}
			}catch (IndexOutOfBoundsException e) {
				// TODO: handle exception
				System.out.println("尾页不够pageSize了，但是不影响输出");
			}
		
		
		//用学生学号从数据库中读取对应的单个学生的信息
		student = studentDAO.get(stunum);
		
		CollegeDAOImpl clgnamedao = new CollegeDAOImpl();
		String clgname = clgnamedao.get(student.getSpenum()).getClgname();
		
		request.setAttribute("student", student);
		request.setAttribute("clgname", clgname);
		request.setAttribute("cruObjects", cruObjects);
    	request.setAttribute("PageService", Pageservice1);
		return "getsuccess";
	}
		
	/**
	 * 获取在全校学生的信息，过多再在前后端进行筛�?
	 * @return
	 */
	public String list(){
		students = studentDAO.list();
		ActionContext.getContext().getSession().put("students", students);
		return "success";
	}
	/**
	 * 删除学生信息
	 */
	public String delete(){
		//根据jsp的stunum值删除对应学生信�?
		studentDAO.delete(student.getStunum());
		Map session = ActionContext.getContext().getSession();
		session.remove("students");
		students = studentDAO.list();
		ActionContext.getContext().getSession().put("students", students);
		//重新获得数据库内的所有的学生信息再重新赋值给students以便于jsp输出
		return "deletesuccess";
	}
	/**
	 * 修改学生信息
	 */
	public String update(){
		//获取session�?
		Map session = ActionContext.getContext().getSession();
		//获取session域中的student对象
		Student student1 = (Student)session.get("student");
		//获取request域中的stupad�?
		String stupad = student.getStupad();
		//用request域中的stupad值替换session域中的student的stupad�?
		student1.setStupad(stupad);
		//调用student的update方法更新stupad
		studentDAO.update(student1);
		//清除session中的students列表，以转向studentupdateview页面展示被修改后的�?
		Long num = student1.getStunum();
		session.remove("student");
		student = studentDAO.get(num);
		ActionContext.getContext().getSession().put("student", student);
		return "success";
	}
	/**
	 * 添加学生信息
	 */
	public String add(){
		studentDAO.add(student);
		Map session = ActionContext.getContext().getSession();
		session.remove("students");
		students = studentDAO.list();
		ActionContext.getContext().getSession().put("students", students);
		return "addsuccess";
	}

	@Override
	public void setSession(Map<String, java.lang.Object> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public TSObject gettSObject() {
		return tSObject;
	}
	public void settSObject(TSObject tSObject) {
		this.tSObject = tSObject;
	}
	public List<TSObject> gettSObjects() {
		return tSObjects;
	}
	public void settSObjects(List<TSObject> tSObjects) {
		this.tSObjects = tSObjects;
	}
	public int getCurrentPagechange() {
		return currentPagechange;
	}
	public void setCurrentPagechange(int currentPagechange) {
		this.currentPagechange = currentPagechange;
	}
	public TSObject getObject() {
		return tSObject;
	}

	public void setObject(TSObject tSObject) {
		this.tSObject = tSObject;
	}

	public ObjectDAOImpl getObjectDAO() {
		return objectDAO;
	}

	public void setObjectDAO(ObjectDAOImpl objectDAO) {
		this.objectDAO = objectDAO;
	}

	public List<TSObject> getObjects() {
		return tSObjects;
	}

	public void setObjects(List<TSObject> tSObjects) {
		this.tSObjects = tSObjects;
	}

	public CRUObject getCruObject() {
		return cruObject;
	}

	public void setCruObject(CRUObject cruObject) {
		this.cruObject = cruObject;
	}

	public List<CRUObject> getCruObjects() {
		return cruObjects;
	}

	public void setCruObjects(List<CRUObject> cruObjects) {
		this.cruObjects = cruObjects;
	}

	public CRUObjectService getCruObjectService() {
		return cruObjectService;
	}

	public void setCruObjectService(CRUObjectService cruObjectService) {
		this.cruObjectService = cruObjectService;
	}

	public List<Obj_Stu> getObj_Stus() {
		return obj_Stus;
	}

	public void setObj_Stus(List<Obj_Stu> obj_Stus) {
		this.obj_Stus = obj_Stus;
	}

	public Obj_Stu getObj_Stu() {
		return obj_Stu;
	}

	public void setObj_Stu(Obj_Stu obj_Stu) {
		this.obj_Stu = obj_Stu;
	}

	public Obj_StuDAOImpl getObj_StuDAO() {
		return obj_StuDAO;
	}

	public void setObj_StuDAO(Obj_StuDAOImpl obj_StuDAO) {
		this.obj_StuDAO = obj_StuDAO;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}
	
	public StudentDAO getStudentDAO() {
		return studentDAO;
	}

	public void setStudentDAO(StudentDAO studentDAO) {
		this.studentDAO = studentDAO;
	}

	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}

}
